﻿package src.libCom
{
	import debugs.Debug;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import net.netvars;
	/**
	 * ...
	 * @author keys_star@163.com
	 */
	public class FixLib extends EventDispatcher
	{
		private var _ID:uint = 0;
		private var LIB:XML;
		public var ID:uint = 0;
		private var total:uint = 2;
		public function FixLib() 
		{
			fixNextTitle();
		}
		public function init(id:uint = 0) {
			total = 2;
			_ID = 0;
			var ids = id;
			netvars.setVar("MONEY", 0);
			Debug.send(this + ".ID: " + id);
			netvars.setVar("ID", ids);
			
			var flvURL:String = "QALIB/" + "LIB" + id + "/v.flv";
			netvars.setVar("flvURL", flvURL);
			var req:URLRequest = new URLRequest("QALIB/" + "LIB" + id+"/lib.xml");
			var uload:URLLoader = new URLLoader(req);
			uload.addEventListener(Event.COMPLETE, ulcom);
			
			var req0:URLRequest = new URLRequest("QALIB/" + "LIB" + id + "/list.xml");
			var uload0:URLLoader = new URLLoader(req0);
			uload0.addEventListener(Event.COMPLETE, ulcom0);
		}
		private function ulcom(e:Event) {
			LIB = new XML(e.target.data);
			var list:XMLList = LIB.item;
			netvars.setVar("LIBLEN", length);
			fixNextAnswer();
			check(e);
		}
		private function ulcom0(e:Event) {
			var lrcXML:XML = new XML(e.target.data);
			var flrc:FixLrc = new FixLrc(lrcXML);
			var arr = flrc.LRCITEM;
			netvars.setVar("LrcItem", arr);
			//Debug.send(this + ".lrcItem: " + arr);
			check(e);
		}
		private function check(e:Event) {
			total-=1;
			if (total<=0) {
				dispatchEvent(e);
			}
		}
		public function get length():uint {
			var list:XMLList = LIB.item;
			return list.length();
		}
		public function getAnswer(id:uint = 0) {
			//var arr:Array;
			try{
				var n = Number(netvars.getVar("MONEY"));
				if (!n) {
					netvars.setVar("MONEY", 1000);
				}else {
					netvars.setVar("MONEY", n + 500);
					if (Number(netvars.getVar("MONEY")) > 5000) {
						netvars.setVar("MONEY", 5000);
					};
				}
			}catch (e:Error) {
				netvars.setVar("MONEY", 1000);
			}
			Debug.send(this + ".money.init");
			netvars.setVar("LIBID", id);
			Debug.send(this + ".LIBID: " + id);
			var idCard = LIB.item[id].IdCard.toString();
			var idSound = LIB.item[id].Sound.toString();
			var arr:Array = [idCard, idSound];
			netvars.setVar("IDCARD", idCard);
			netvars.setVar("IDSOUND", idSound);
			return arr;
		}
		//下一个接入的答案
		public function fixNextAnswer() {
			//Debug.send(this + ".nextAnswer");
			var t = getAnswer(_ID++);
			return t;
		}
		//下一个标题
		public function fixNextTitle() {
			//Debug.send(this + ".nextTitle");
			var len=netvars.getVar("length");
			if (ID >= len) {
				ID = 0;
			}
			init(ID++);
		}
	}
}